USE [DSRSystem_Temp]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spGetCompanyKeyESGIssuesByCompanyIds]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spGetCompanyKeyESGIssuesByCompanyIds]
GO

CREATE PROCEDURE [dbo].[spGetCompanyKeyESGIssuesByCompanyIds]
	@CompanyIds NVARCHAR(MAX)
AS
BEGIN
	SET NOCOUNT ON;

	DECLARE @XML XML
	SET @XML = CONVERT(XML, @CompanyIds, 1)

	DECLARE @SelectedCompanies TABLE
	(
		Id uniqueidentifier
	)
	INSERT INTO @SelectedCompanies
	SELECT TID.id.value('.','uniqueidentifier') AS Id
	FROM @XML.nodes('/CompanyIds') AS T(c) outer apply T.c.nodes('Id') as TID(id)

	SELECT	CompanyKeyESGIssues.Id,
			CompanyKeyESGIssues.CompanyId,
			CompanyKeyESGIssues.CompanyName, 
			CompanyKeyESGIssues.SubIndustryCode, 
			CompanyKeyESGIssues.Name, 
			CompanyKeyESGIssues.ExternalId, 
			CompanyKeyESGIssues.Description, 
			CompanyKeyESGIssues.Comment, 
			CompanyKeyESGIssues.SortKey
	FROM dbo.CompanyKeyESGIssues AS CompanyKeyESGIssues
	INNER JOIN @SelectedCompanies AS SelectedCompanies ON SelectedCompanies.Id = CompanyKeyESGIssues.CompanyId
END
GO


